//图表
var pieChart = function(myChart, data){
	var legend = [];
	var series = [];
	for(var item in data.data){
		legend.push(item);
		series.push({value:data.data[item], name:item});
	}
	
	var option = {
	    title : {
	        text: data.name,
	        x:'center'
	    },
	    animation: false,
	    tooltip : {
	        trigger: 'item',
	        formatter: "{a} <br/>{b} : {c} ({d}%)"
	    },
	    legend: {
	        orient : 'vertical',
	        x : 'left',
	        data:legend
	    },
	    toolbox: {
	        show : false,
	        feature : {
	            mark : {show: true},
	            dataView : {show: true, readOnly: false},
	            magicType : {
	                show: true, 
	                type: ['pie', 'funnel'],
	                option: {
	                    funnel: {
	                        x: '25%',
	                        width: '50%',
	                        funnelAlign: 'left',
	                        max: 1548
	                    }
	                }
	            },
	            restore : {show: true},
	            saveAsImage : {show: true}
	        }
	    },
    	calculable : true,	
	    series : [
	        {
	            name:'访问来源',
	            type:'pie',
	            radius : '55%',
	            center: ['50%', '60%'],
	            data:series
	        }
	    ]
	};
	
	myChart.setOption(option);
	
	//点击导出报告
	return myChart.getDataURL();
}

var tableData = function(allData){
	//表格数据填充
	var tableId = 0;//表格ID
	for(var tIndex in allData){
		var data = allData[tIndex];//每个表格数据
		var initAssetId = -1;//初始化资产Id
		var initData = {};
		var count = 1, assetCount = 1, showHTML = "";
		for(var trIndex in data){
			isAccept["data"][data[trIndex]["isAccept"]] ++;
			riskLevel["data"][data[trIndex]["riskLevel"]] ++;
			if(initAssetId != data[trIndex]["assetId"]){
				initAssetId = data[trIndex]["assetId"];
				if(trIndex == 0){
					initData = data[trIndex];
				}else{
					//显示
					var initHTML =  "<tr><td rowspan="+assetCount+">"+count+"</td>"+
									"<td rowspan="+assetCount+">"+initData["assetName"]+"</td>"+
									"<td rowspan="+assetCount+">"+initData["importance"]+"</td>"+
									"<td>"+initData["weakName"]+"</td>"+
									"<td>"+initData["severity"]+"</td>"+
									"<td>"+initData["frequency"]+"</td>"+
									"<td>"+initData["riskValue"]+"</td>"+
									"<td>"+initData["isAccept"]+"</td>"+
									"<td>"+initData["riskLevel"]+"</td></tr>";

					$("#"+tableId).append(initHTML + showHTML);
					//清空，资产计数加1
					count ++;//资产计数
					assetCount = 1;
					showHTML = "";
					initData = data[trIndex];
				}
			}else{
				assetCount ++;//同一资产下威胁计数
				showHTML += "<tr><td>"+data[trIndex]["weakName"]+"</td>"+
							"<td>"+data[trIndex]["severity"]+"</td>"+
							"<td>"+data[trIndex]["frequency"]+"</td>"+
							"<td>"+data[trIndex]["riskValue"]+"</td>"+
							"<td>"+data[trIndex]["isAccept"]+"</td>"+
							"<td>"+data[trIndex]["riskLevel"]+"</td></tr>";
			}
		}
		
		//最后的数据显示
		var HTML =  "<tr><td rowspan="+assetCount+">"+count+"</td>"+
						"<td rowspan="+assetCount+">"+initData["assetName"]+"</td>"+
						"<td rowspan="+assetCount+">"+initData["importance"]+"</td>"+
						"<td>"+initData["weakName"]+"</td>"+
						"<td>"+initData["severity"]+"</td>"+
						"<td>"+initData["frequency"]+"</td>"+
						"<td>"+initData["riskValue"]+"</td>"+
						"<td>"+initData["isAccept"]+"</td>"+
						"<td>"+initData["riskLevel"]+"</td></tr>" + showHTML;

		$("#"+tableId).append(HTML);
		tableId ++;
	}
}